<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('设备申领列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>审批进度：</label>
                            <select name="status" th:with="type=${@dict.getType('dev_apply_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li class="select-time">
                            <label style="width: 105px;">申请时间：</label>
                            <input type="text" class="time-input" id="startTime" placeholder="开始时间"
                                   name="startTime"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间"
                                   name="endTime"/>
                        </li>
                        <li>
                            <label style="width: 105px;">关键字查询字段：</label>
                            <select name="fields">
                                <option value="trueName" selected>姓名</option>
                                <option value="partnerName">申请者(合作商)</option>
                                <option value="phone">手机号</option>
                                <option value="addressId">地址</option>
                            </select>
                            <input type="text" name="keywords"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:devApply:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:devApply:remove')}]];
    var refuseFlag = [[${@permission.hasPermi('system:devApply:refuse')}]];
    var adoptShFlag = [[${@permission.hasPermi('system:devApply:adoptSh')}]];
    var deliverGoodsFlag = [[${@permission.hasPermi('system:devApply:deliverGoods')}]];
    var statusDatas = [[${@dict.getType('dev_apply_status')}]];
    var prefix = ctx + "system/devApply";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "设备申领",
            detailView: true,
            onExpandRow: function (index, row, $detail) {
                initChildTable(index, row, $detail);
            },
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '',
                    visible: false
                },
                {
                    field: 'partnerName',
                    title: '申请者(合作商)'
                },
                {
                    field: 'createTime',
                    title: '申请时间'
                },
                {
                    field: 'parentId',
                    title: '审批者',
                    formatter: function (value, row, index) {
                        var val = "";
                        if (value == null || value == "") {
                            val = "<span style='color: black;'>平台</span>";
                        } else {
                            val = row.superiorName + "<span style='color: green;'>(合作商)</span>";
                        }
                        return val;
                    }
                },
                {
                    field: 'trueName',
                    title: '姓名'
                },
                {
                    field: 'phone',
                    title: '手机号'
                },
                {
                    field: 'address',
                    title: '地址',
                    formatter: function (value, row, index) {
                        return row.province +"-"+ row.city + "-" + row.district + "-" + row.detail;
                    }
                },
                {
                    field: 'voucher',
                    title: '凭证',
                    formatter: function (value, row, index) {
                        if (value == null || value == "") {
                            return "";
                        } else {
                            return $.table.imageView(value);
                        }
                    }
                },
                {
                    field: 'status',
                    title: '审批进度',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'approvalTime',
                    title: '审批通过时间'
                },
                {
                    field: 'deliveryTime',
                    title: '发货时间'
                },
                {
                    field: 'remarks',
                    title: '备注'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];

                        if (row.parentId == null || row.parentId == "") {
                            // 向平台申领

                            /**
                             * 审批进度
                             0 未审批
                             1 待发货
                             2 已发货
                             3 已拒绝
                             */
                            if (row.status == 0) {
                                // 未审批
                                actions.push('<a class="btn btn-danger btn-xs ' + refuseFlag + '" href="javascript:void(0)" onclick="refuseSp(\'' + row.id + '\')"><i class="fa fa-close"></i>拒绝审批</a> ');

                                // 审批通过
                                actions.push('<a class="btn btn-success btn-xs ' + adoptShFlag + '" href="javascript:void(0)" onclick="adoptSh(\'' + row.id + '\')"><i class="fa fa-battery"></i>通过审核</a> ');
                            }
                            if (row.status == 1) {
                                // 确认发货
                                actions.push('<a class="btn btn-success btn-xs ' + deliverGoodsFlag + '" href="javascript:void(0)" onclick="deliverGoods(\'' + row.id + '\')"><i class="fa fa-pagelines"></i>确认发货</a> ');
                            }

                        }
                        // actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        // actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function adoptSh(id) {
        $.modal.confirm("确认审批通过？,温馨提示：审核通过后,请到设备管理处进行划分设备操作！", function () {
            var url = prefix + "/adoptSh";
            var data = {"id": id};
            $.operate.submit(url, "post", "json", data);
        });
    }


    function deliverGoods(id) {
        $.modal.confirm("确认发货？", function () {
            var url = prefix + "/deliverGoods";
            var data = {"id": id};
            $.operate.submit(url, "post", "json", data);
        });
    }

    function refuseSp(id) {
        var url = prefix + "/refuseSp/" + id;
        $.modal.open("拒绝申请", url, 800, 600);
    }

    initChildTable = function (index, row, $detail) {
        // 展示详情

        var devTypeDatas = [[${@dict.getType('sys_device_type')}]];
        var childTable = $detail.html('<table style="table-layout:fixed"></table>').find('table');
        $(childTable).bootstrapTable({
            url: prefix + "/itemslist",
            method: 'post',
            sidePagination: "server",
            contentType: "application/x-www-form-urlencoded",
            queryParams: {
                devApplyId: row.id
            },
            columns: [
                {
                    field: 'id',
                    title: '',
                    visible: false
                },
                {
                    field: 'itemImg',
                    title: '商品图片',
                    formatter: function (value, row, index) {
                        if (value == "" || value == null) {
                            return "-";
                        } else {
                            return $.table.imageView(value);
                        }
                    }
                },
                {
                    field: 'itemName',
                    title: '商品名称'
                },
                {
                    field: 'buyCounts',
                    title: '购买数量'
                },
                {
                    field: 'devType',
                    title: '设备类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(devTypeDatas, value);
                    }
                },
            ]
        });
    };


</script>
</body>
</html>